<?php
/*
Primary Developer: Derek Shue
Updated: 12/10/2011
Module Description: CoursesQuery provides an interface for Course-related database queries.
*/
class Application_Model_CoursesQuery
{
    public function init(){
	}
		
	/**
	* Returns courses that should be visible to as student.
	* ie all courses in current and future terms.
	*/
    public function getCourse_ID($cid){
		$courses = new Application_Model_DbTable_Courses();
		$query = 'CID = '.$cid;
		try{
			$row = $courses->fetchRow($query);
		}
		catch(Exception $e){
			$row = Null;
		}
		return $row->toArray();
    }	
		
	/**
	* Returns courses that should be visible to as student.
	* --all courses in current term.
	*/
    public function getCourses_Student(){
		$term = new Application_Model_Term();
        $current_term = $term->getCurrentOpenTermStr('stuapp');
		$courses = new Application_Model_DbTable_Courses();
		$query = 'TERM = \'' . $current_term.'\'';
		try{
			$rows = $courses->fetchAll($query);
		}
		catch(Exception $e){
			$rows = Null;
		}
		return $rows;
    }
	
	/**
	* Returns courses that should be visible to an admin for the reqeusted terms
	*/
	public function getCoursesForTerm_Admin($terms){
		$courses = new Application_Model_DbTable_Courses();
		$query = 'TERM in '.$terms;
		try{
			$rows = $courses->fetchAll($query)->toArray();
		}
		catch(Exception $e){
			$rows = Null;
		}
		return $rows;
	}
	
	/**
	* Returns courses that should be visible to a teacher in default view
	* ie all current and future terms belonging to that teacher (TEACHERID = $teacherid)
	*/
	public function getCoursesForTerm_Teacher($teacherid, $term){
		$courses = new Application_Model_DbTable_Courses();
		$query = 'TEACHERID = '. $teacherid . ' AND TERM = \'' . $term.'\'';
		try{
			$rows = $courses->fetchAll($query);
		}
		catch(Exception $e){
			$rows = Null;
		}
		return $rows;
    }
    /**
    * Returns courses that should be visible to an admin in default view
    * ie all current and future terms
    */
    public function getCourses_Admin(){
    	$term = new Application_Model_Term();
    	$current_terms = $term->getCurrentFutureTermsStr_admin();
    	return $this->getCoursesForTerm_Admin($current_terms);
    }
    
    /**
     * Returns courses that should be visible to a teacher in default view
     * ie all current and future terms belonging to that teacher (TEACHERID = $teacherid)
     */
    public function getCourses_Teacher($teacherid){
    	$term = new Application_Model_Term();
    	$current_term = $term->getCurrentOpenTermStr('facCourse');
    	return $this->getCoursesForTerm_Teacher($teacherid, $current_term);
    }
	
	/**
	* Returns courses that should be visible to an admin given the query data
	* inputterm and termmodifer behave as defined in application_model_term
	*/
	public function selectCourses_Teacher($teacherid, $inputterm, $termmodifier, $coursename, $coursenumber){
		$term = new Application_Model_Term();
		$current_term = $term->getCurrentTerm();
		if($inputterm['term']){
			$current_term['term'] = $inputterm['term'];
		}
		if($inputterm['year']){
			$current_term['year'] = $inputterm['year'];
		}
		$courses = new Application_Model_DbTable_Courses();
		$query = "CID >= 1 ";
		if($coursename)
			$query = $query .  " AND COURSENAME LIKE '%$coursename%'";
		if($coursenumber)
			$query = $query .  " AND COURSENUMBER = $coursenumber ";
		if($teacherid)
			$query = $query. "AND TEACHERID = $teacherid";
		try{
			$term_list = $term->getTerms($inputterm, $termmodifier);
		}
		catch(Exception $e){
			return Null;
		}	
		if($current_term)
			$query = $query . 'AND TERM  IN '. $term_list;
		try{
			$rows = $courses->fetchAll($query);
		}
		catch(Exception $e){
			$rows = Null;
		}
		return $rows;
    }
    
	/**
	* Returns courses that should be visible to a student given the query data
	* inputterm and termmodifer behave as defined in application_model_term
	*/
	public function selectCourses_Student($inputterm, $termmodifier, $coursename, $coursenumber){
		$term = new Application_Model_Term();
		$current_term = $term->getCurrentOpenTerm('stuapp');
		if($inputterm['term']){
			$current_term['term'] = $inputterm['term'];
		}
		if($inputterm['year']){
			$current_term['year'] = $inputterm['year'];
		}
		$courses = new Application_Model_DbTable_Courses();
		$query = "CID >= 1 ";
		if($coursename)
			$query = $query .  " AND COURSENAME LIKE '%$coursename%'";
		if($coursenumber)
			$query = $query .  " AND COURSENUMBER = $coursenumber ";			
		try{
			$term_list = $term->getTerms($inputterm, $termmodifier);
		}
		catch(Exception $e){
			return Null;
		}		
		if($current_term)
			$query = $query . 'AND TERM  IN '. $term_list;
		try{
			$rows = $courses->fetchAll($query);
		}
		catch(Exception $e){
			$rows = Null;
		}
		return $rows;
         }
    }

?>
